package com.fort.teachermanagementsys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fort.teachermanagementsys.entity.SysMenu;
import com.fort.teachermanagementsys.mapper.SysMenuMapper;
import com.fort.teachermanagementsys.service.SysMenuService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Allen
 * @since 2024-04-11
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public boolean addSysMenu(SysMenu sysMenu) {
        if (sysMenuMapper.insert(sysMenu) == 1) {
            sysMenuMapper.insert(sysMenu);
            return true;
        }
        return false;
    }


    public List<SysMenu> menuList(){
        List<SysMenu> sysMenuList = new ArrayList<>();
        //获取全部菜单
        sysMenuList = sysMenuMapper.selectList(null);
        //获取根菜单
        List<SysMenu> parentMenuList = sysMenuList.stream().filter(sysMenu -> sysMenu.getParentId() == 0).collect(Collectors.toList());

        for (SysMenu parentMenu : parentMenuList) {
            recursionMenu(parentMenu,sysMenuList);
        }
        parentMenuList = parentMenuList.stream().sorted(Comparator.comparing(SysMenu::getOrderNum)).collect(Collectors.toList());
        return parentMenuList;
    }

    private void recursionMenu(SysMenu parentMenu, List<SysMenu> sysMenuList) {
        //拿到父菜单下所有的子菜单
        List<SysMenu> chlidrenMenus = sysMenuList
                .stream()
                .filter(u -> u.getParentId() != 0)
                .filter(sysMenu -> sysMenu.getParentId() == parentMenu.getId())
                .collect(Collectors.toList());
        parentMenu.setChildrenMenus(chlidrenMenus);
        for (SysMenu sysMenu : chlidrenMenus) {
            recursionMenu(sysMenu,sysMenuList);
        }

    }

//    // 添加菜单
//    @Transactional
//    public void addMenu(SysMenu menu) {
//        menu.setCreateTime(LocalDateTime.now());
//        menu.setUpdateTime(LocalDateTime.now());
//        menuMapper.insert(menu);
//    }

    // 更新菜单
    @Transactional
    public void updateMenu(SysMenu menu) {
        menu.setUpdateTime(LocalDateTime.now());
        sysMenuMapper.updateById(menu);
    }

    // 删除菜单
    @Transactional
    public void deleteMenu(Integer id) {
        // 检查是否有子菜单
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Long count = sysMenuMapper.selectCount(wrapper);
        if (count > 0) {
            throw new RuntimeException("存在子菜单，无法删除");
        }
        sysMenuMapper.deleteById(id);
    }

    // 根据 ID 查询菜单
    public SysMenu getMenuById(Integer id) {
        return sysMenuMapper.selectById(id);
    }


}
